package com.xique.client.controller;

import com.xique.client.bean.request.*;
import com.xique.client.service.biz.service.IClientCommandService;
import com.xique.client.service.biz.service.IClientHomeBizService;
import com.xique.client.service.biz.service.IWxAppClientService;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author shd
 * @create 2021/11/5 9:19
 */
@RestController
@RequestMapping("/app/client")
@Slf4j
public class WxAppClientController extends BaseController {

    @Autowired
    private IWxAppClientService wxAppClientService;

    @Autowired
    private IClientHomeBizService clientHomeBizService;

    @Autowired
    private IClientCommandService clientCommandService;

    /**
     * 道闸控制（开闸）
     * @return
     */
    @PostMapping("/openGate")
    @ResponseBody
    @ApiOperation("开闸")
    @Log(title = "开闸", businessType = BusinessType.COMMAND)
    public AjaxResult openGate(@Validated  @RequestBody ClientOprGateRequest request){
        return AjaxResult.success(clientCommandService.openGate(request));
    }

    /**
     * 道闸控制（关闸）
     * @return
     */
    @PostMapping("/closeGate")
    @ResponseBody
    @ApiOperation("关闸")
    @Log(title = "关闸", businessType = BusinessType.COMMAND)
    public AjaxResult closeGate(@Validated @RequestBody ClientOprGateRequest request){
        return AjaxResult.success(clientCommandService.closeGate(request));
    }

    /**
     * 岗亭首页信息
     * @author shd
     * @date 2021/7/29 10:34
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @GetMapping("/homePageInfo")
    @ApiOperation("岗亭首页")
    public AjaxResult homePageInfo() {
        return wxAppClientService.homePageInfo();
    }

    /**
     * 首页通道最新出场订单信息
     * @author shd
     * @date 2021/11/9 9:48
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @GetMapping("/orderInfo")
    @ApiOperation("最新出场订单信息")
    public AjaxResult orderInfo(WxAppClientLastOrderInfoRequest request) {
        return wxAppClientService.orderInfo(request);
    }

    /**
     * 改变车型
     * @author shd
     * @date 2021/11/9 14:54
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @PostMapping("/changeCarType")
    @ApiOperation("改变车型")
    public AjaxResult changeCarType(@Validated @RequestBody WxAppChangeCarTypeRequest request) {
        return wxAppClientService.changeCarType(request);
    }

    /**
     * 入口确认放行
     * @author shd
     * @date 2021/11/24 10:46
     * @param request
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @PostMapping("/inLanePass")
    @ApiOperation("入口确认放行")
    @Log(title = "入口确认放行", businessType = BusinessType.COMMAND)
    public AjaxResult inLanePass(@Validated @RequestBody WxAppInLanePassRequest request) {
        return wxAppClientService.inLanePass(request);
    }

    /**
     * 入口取消放行
     * @author shd
     * @date 2021/11/24 11:48
     * @param request
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @PostMapping("/inLaneCancel")
    @ApiOperation("入口取消放行")
    @Log(title = "入口取消放行", businessType = BusinessType.COMMAND)
    public AjaxResult inLaneCancel(@Validated @RequestBody WxAppInLaneCancelRequest request) {
        return wxAppClientService.inLaneCancel(request);
    }

    /**
     * 根据ip获取地址信息
     * @author shd
     * @date 2021/12/1 16:14
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @GetMapping("/getAddressByIp")
    @ApiOperation("根据ip获取地址信息")
    public AjaxResult getAddressByIp() {
        return wxAppClientService.getAddressByIp();
    }


    @PostMapping("/handCarInOrOut")
    @ResponseBody
    @ApiOperation("手动进出场")
    @Log(title = "手动进出场", businessType = BusinessType.COMMAND)
    public AjaxResult handCarInOrOut(@Validated @RequestBody ClientHandInOutRequest request){

//        boolean matches = request.getPlate().matches("[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[警京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{0,1}[A-HJ-NP-Z0-9]{4}[A-Z0-9挂学警港澳]{1,2}$");
        boolean matches = StringUtils.isLegalPlate(request.getPlate());
        if (!matches){
            return  AjaxResult.error("车牌格式不正确!");
        }
        return AjaxResult.success(clientHomeBizService.handCarInOrOut(request));
    }

    @PostMapping("handOut/parkOnList")
    @ApiOperation("手动出场根据车牌号获取在场车辆列表")
    public AjaxResult handOutParkOnList(@Validated @RequestBody HandOutParkOnListRequest request){
        return clientHomeBizService.getHandOutParkOnList(request);
    }

    @GetMapping("handIn/isAllowEditInTime")
    @ApiOperation("手动入场是否允许修改入场时间")
    public AjaxResult isAllowEditInTime(){
        return clientHomeBizService.isAllowEditInTime();
    }

    @GetMapping("freePass/reason")
    @ApiOperation("获取免费放行原因列表")
    public AjaxResult getFreePassReason(){
        return clientHomeBizService.getFreePassReason();
    }

    @GetMapping("openGate/reason")
    @ApiOperation("获取开闸原因列表")
    public AjaxResult getOpenGateReason(){
        return clientHomeBizService.getOpenGateReason();
    }

    @GetMapping("closeGate/reason")
    @ApiOperation("获取关闸原因列表")
    public AjaxResult getCloseGateReason(){
        return clientHomeBizService.getCloseGateReason();
    }
}
